package asia.doing.controller.user;

import asia.doing.common.Result;
import asia.doing.entity.Category;
import asia.doing.service.user.FrontCategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController(value = "frontCategoryController")
@RequestMapping("/front/category")
public class FrontCategoryController {
    @Resource
    private FrontCategoryService frontCategoryService;

    @PostMapping("/add")
    public Result add(@RequestBody Category category){
        frontCategoryService.add(category);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id){
        frontCategoryService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        frontCategoryService.deleteBatch(ids);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateById(@RequestBody Category category){
        frontCategoryService.updateById(category);
        return Result.success();
    }


    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id){
        Category category = frontCategoryService.selectById(id);
        return Result.success(category);
    }

    @GetMapping("/selectAll")
    public Result selectAll(Category category){
        List<Category> list = frontCategoryService.selectAll(category);
        return Result.success(list);
    }

    @GetMapping("/selectPage")
    public Result selectAll(Category category,
                            @RequestParam(defaultValue = "1") Integer pagNum,
                            @RequestParam(defaultValue = "10") Integer pagSize){
        PageInfo<Category> list = frontCategoryService.selectPage(category,pagNum,pagSize);
        return Result.success(list);
    }
}
